package com.team.game.framework.impl;


import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.team.game.framework.Game;

@SuppressLint("ViewConstructor")
public class Surface extends SurfaceView implements SurfaceHolder.Callback {
    private final Game game;
    private SurfaceThread drawThread;

    private static final String TAG = "Surface";

    public Surface(Context context, Game game) {
        super(context);
        this.game = game;
        getHolder().addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        Log.i (TAG, "created");
        drawThread = new SurfaceThread (getHolder (), game);
        drawThread.setRunning(true);
        drawThread.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
        Log.i (TAG, "changed");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        Log.i (TAG, "destroy");
        boolean retry = true;
        // завершаем работу потока
        drawThread.setRunning(false);
        while (retry) {
            try {
                drawThread.join();
                retry = false;
            } catch (InterruptedException e) {
                // если не получилось, то будем пытаться еще и еще о да детка
            }
        }

    }


}
